package com.sky.web.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("page")
    public Result findByPage(CategoryPageQueryDTO categoryDTO){
       PageResult pageResult = categoryService.findByPage(categoryDTO);
       return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO){
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @DeleteMapping
    public Result deleteById(@RequestParam Long id){
            categoryService.deleteById(id);
        return Result.success();
    }

    @GetMapping("list")
    public Result findByList(Integer type){
        List<Category> list =categoryService.findByList(type);
        return Result.success(list);
    }

    @PostMapping("status/{status}")
    public Result updateStatus(@PathVariable Integer status ,@RequestParam Long id){
        categoryService.updateStatus(status,id);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
                categoryService.update(categoryDTO);
        return Result.success();
    }
}
